package com.stars.controller.mygenerate;/**
 * Created by luodajie on 2019/1/6.
 */

import com.github.pagehelper.PageInfo;
import com.stars.common.base.ResultMsg;
import com.stars.controller.advice.CommonExceptionHandler;
import com.stars.entity.mygenerate.CgFormHead;
import com.stars.entity.mygenerate.command.CgFormHeadCommand;
import com.stars.services.mygenerate.CgFormHeadServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

/**
 * @author :dj
 * @version : 1.0.0
 * @description
 * @Date 2019-01-06 15:47
 * @note
 */
@Slf4j
@RestController
@RequestMapping("/myGenerate")
@Api(tags="项目代码生成工具")
public class MyGenerateCtl extends CommonExceptionHandler {
    @Autowired
    private CgFormHeadServices cgFormHeadServices;

    @GetMapping(value = "/goAddCgFormHead")
    public ModelAndView goAdd(ModelMap modelMap) {
        CgFormHead cgFormHead = new CgFormHead();
        String dataJson =" [" +
                " {name:\"id\",text:\"id\",length:36},\n" +
                " {name:\"name\",text:\"名称\",length:50},\n" +
                " {name:\"age\",type:\"int\",text:\"名称\",length:36},\n" +
                " {name:\"create_date\",type:\"long\",text:\"创建时间\"},\n" +
                " {name:\"create_date2\",type:\"date\",text:\"创建时间2\"}\n" +
                " ]";
        cgFormHead.setDataJson(dataJson);
        modelMap.put("cgFormHead", cgFormHead);

        modelMap.put("resultMsg", new ResultMsg());
        return new ModelAndView("addCgFormHead", modelMap);
    }

    @GetMapping(value = "/goVue")
    public ModelAndView goVue(ModelMap modelMap) {
        modelMap.put("msg", "hello Dj");
        return new ModelAndView("iview", modelMap);
    }
    @GetMapping(value = "/cgHeadList")
    public ModelAndView cgHeadList(ModelMap modelMap) {
        modelMap.put("msg", "hello Dj");
        return new ModelAndView("cgHeadList", modelMap);
    }
    @GetMapping("/list")
    public PageInfo<CgFormHead> pageCgFormHead(int pageNum, int pageSize, CgFormHeadCommand cgFormHead) throws Exception {
        return cgFormHeadServices.pageQuery(pageNum, pageSize, cgFormHead);
    }

    @PostMapping("/add")
    public ResultMsg addCgformHead(@Valid CgFormHead cgFormHead) throws Exception {
       return   cgFormHeadServices.add(cgFormHead);
    }

    @PostMapping("/update")
    public ResultMsg updateCgformHead(@Valid CgFormHead cgFormHead) throws Exception {
        return cgFormHeadServices.update(cgFormHead);
    }

    @PostMapping("/delById")
    public ResultMsg delById(String ids) throws Exception {
       return cgFormHeadServices.delById(ids);
    }

    @GetMapping("/findById")
    public ResultMsg findById(String id) {
        CgFormHead cgFormHead = cgFormHeadServices.findById(id);
        return ResultMsg.success(cgFormHead);
    }

    @GetMapping("/createDBTable")
    public ResultMsg createDBTable(String id) {
        try {
            return cgFormHeadServices.createDBTable(id);
        } catch (Exception ex) {
            return ResultMsg.failure(ex.getMessage());
        }
    }
}
